package datastructure.linkedlist;

/**
 * 约瑟夫问题的实现
 * @author lbw
 */
public class Josephus {

    public static void main(String[] args) {
        Josephus josephus = new Josephus(100, 5);

    }

    /**
     *
     * @param n n个人
     * @param m 所报的数
     */
    public Josephus(int n, int m) {
        IntNode head = new IntNode();
        IntNode cur = head;
        for (int i = 1; i <= n; i++) {
            IntNode node = new IntNode();
            node.data = i;
            cur.next = node;
            cur = cur.next;
        }
        cur.next = head.next;

        IntNode node = head.next;
        while (node.next != node){
            for (int i = 1; i < m-1; i++) {
                node = node.next;
            }
            System.out.println(node.next.data + "出局");
            node.next = node.next.next;
            node = node.next;
        }
    }

}

class IntNode{
    public  int data;
    public  IntNode next;

}
